$(function () {

    var init = function (params) {

        //上传 部分
        var coverUrl = "";

        var uploaderCoverUrl = function ($updom, url) {
            coverUrl = url;
            $updom.find('.upresult').attr("src", url);
        };


        $.initWebuploader({
            updom: $("#uploader-coverUrl"),
            label: "上传",
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            },
            success: function (res, $updom) {
                console.log($updom)

                uploaderCoverUrl($updom, res.data)
            }

        })








        var $form = $('form').bootstrapValidator({
            fields: {

                sort: {
                    validators: {
                        notEmpty: {
                            message: '排序不能为空'
                        },
                        regexp: {
                            regexp: /^[0-9]+$/,
                            message: '只能是数字'
                        }
                    }
                },
                url: {
                    validators: {
                        notEmpty: {
                            message: '路径不能为空'
                        }
                    }
                }
            }
        });

        if (params) {

                $form.find('input[name=sort]').val(params.sort)
                $form.find('input[name=url]').val(params.url)
                $form.find('input[name=secondUrl]').val(params.secondUrl)
                uploaderCoverUrl($("#uploader-coverUrl"), params.cover)
        }

        $("#submit").on("click", function () {
            var bootstrapValidator = $form.data('bootstrapValidator');

            bootstrapValidator.validate();

            if (bootstrapValidator.isValid()) {

                if ( coverUrl == "") {
                    $.rootErrorDialog("广告图不能为空", 2000)
                    return
                }

                var param = {
                    sort:  $form.find('input[name=sort]').val(),
                    url:  $form.find('input[name=url]').val(),
                    secondUrl:  $form.find('input[name=secondUrl]').val(),
                    token: window.localStorage.getItem('token'),
                    cover: coverUrl
                }
                var postUrl = '/banner/doInsert';

                if (params) {
                    param.id = params.id
                    postUrl = '/banner/doUpdate';
                }
                var submitLayer = layer.load(0);
                $.restPost(postUrl, param).done(function (msg) {
                    layer.close(submitLayer)
                    $.rootSuccessDialog(msg, 2000, function () {
                        location.hash = '/banner/manage'
                    })

                }).fail(function (msg) {

                    $.rootErrorDialog(msg, 2000)

                })
            }

        })
    };

    if (/edit/.test(location.hash)) {
        $.restPost("/banner/doSelect", {
            token: window.localStorage.getItem('token'),
            id: $.getUrlLocation()
        }).done(function (mess, data) {
            init(data)
        })

    } else {
        init(false)

    }

})